<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

use Illuminate\Support\Facades\DB;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct(){
        require_once(app_path().'/CRedis.php');  
    }

    /**
	 * 错误提示
	 *
	 * @return string[]
	 */
	protected function jsonStatus($msg) {
		return json_encode([ 
				'status' => 0,
				'msg' => $msg,
				'data' => []
		]);
	}

    /**
	 * 校验地址栏传参
	 *
	 * @param string $param        	
	 */
	protected function paramVerifyEmpty($param) {
		$param = explode ( ',', $param );
		foreach ( $param as $item )
			if ($_REQUEST [$item] == '')
				return $item;
		return '';
	}

    /**
	 * 校验地址栏是否传参
	 *
	 * @param string $param        	
	 */
	protected function paramVerifyExist($param) {
		$param = explode ( ',', $param );
		foreach ( $param as $item ){
			if (! isset ( $_REQUEST [$item] )){
				return $item;
			}
		}	
		return '';
	}

    /**
	 * json信息返回数组
     */
    protected function getJsonData($state=0,$message='',$data='')
    {
        $jsonData = [
             'status'  => $state,
             'msg' => $message,
             'data' => $data,
        ];
        return $jsonData;
	}

    public function ptoken($user_id){
    	$user = DB::table('pusers')->where('id',$user_id)->first();
        $token = JWTAuth::fromUser($user);
        return $token;
    }

    public function stoken($user_id){
    	$user = DB::table('susers')->where('id',$user_id)->first();
        $token = JWTAuth::fromUser($user);
        return $token;
    }

    public function ytoken($user_id){
    	$user = DB::table('yusers')->where('id',$user_id)->first();
        $token = JWTAuth::fromUser($user);
        return $token;
    }

    public function pjwtuser(){
    	$user = JWTAuth::parseToken()->authenticate();
        return $user->id;
    }
    
}
